package com.sskj.wallet.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.util.CommonUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WalletDetailBean;
import com.sskj.wallet.bean.bus.WalletAssetBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.WalletPresenter;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.WALLET_FRAGMENT)
public class WalletFragment extends BaseFragment<WalletPresenter> {


    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private final String[] mTitles = {
            App.INSTANCE.getString(R.string.wallet_transferActivity2), App.INSTANCE.getString(R.string.wallet_transferActivity3), App.INSTANCE.getString(R.string.wallet_transferActivity4)
            , App.INSTANCE.getString(R.string.wallet_transferActivity5), App.INSTANCE.getString(R.string.wallet_transferActivity6)};
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private BaseAdapter<WalletDetailBean.ListBean> adapter;

    private int chooseWalletType = 1;
    private WalletAssetBean walletAssetBean;

    private int chooseWallet;//当前tab
    private String canSee = "1";

    @Override
    public int getLayoutId() {
        return R.layout.wallet_fragment_wallet;
    }

    @Override
    public WalletPresenter getPresenter() {
        return new WalletPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (smartRefreshView != null) {
                smartRefreshView.autoRefresh();
            }
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (smartRefreshView != null) {
                smartRefreshView.autoRefresh();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (smartRefreshView != null) {
            smartRefreshView.autoRefresh();
        }
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_walletFragment6));
        ivBack.setVisibility(View.GONE);
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_ASSET_ALL).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_ASSET).withString(Constans.WALLET_TYPE, Constans.WALLET_HUOQI).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_ASSET).withString(Constans.WALLET_TYPE, Constans.WALLET_JIEDAI).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_ASSET).withString(Constans.WALLET_TYPE, Constans.WALLET_LIANGHUA).navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_ASSET_DAPP).navigation());
        viewpager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments));
        slidingTabLayout.setViewPager(viewpager, mTitles, getActivity(), mFragments);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.commonTextEEE))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setFirstDraw(false));
        adapter = new BaseAdapter<WalletDetailBean.ListBean>(R.layout.wallet_item_asset, null, recyclerView, false) {
            @Override
            public void bind(ViewHolder holder, WalletDetailBean.ListBean item) {
                switch (chooseWallet) {
                    case 0:
                        canSee = SPUtil.get(SPConfig.ALL_EYE, "1");
                        break;
                    case 1:
                        canSee = SPUtil.get(SPConfig.HUOQI_EYE, "1");
                        break;
                    case 2:
                        canSee = SPUtil.get(SPConfig.JIEDAI_EYE, "1");
                        break;
                    case 3:
                        canSee = SPUtil.get(SPConfig.LIANGHUA_EYE, "1");
                        break;
                    case 4:
                        canSee = SPUtil.get(SPConfig.DAPP_EYE, "1");
                        break;
                    default:
                        break;
                }
                holder.setText(R.id.tv_coin_name, item.getP_name())
                        .setText(R.id.tv_usable, "1".equals(canSee) ? CommonUtil.dealCoinNum(item.getUsable(), item.getP_name()) : "******")
                        .setText(R.id.tv_locked, "1".equals(canSee) ? CommonUtil.dealCoinNum(item.getFrost(), item.getP_name()) : "******")
                        .setText(R.id.tv_cny, "1".equals(canSee) ? item.getCny_price() : "******");
            }
        };
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
            if (adapter.getData() == null || adapter.getData().size() == 0) {
                return;
            }
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_RECORD_ASSET)
                    .withInt(Constans.WALLET_TYPE, chooseWalletType)
                    .withString(Constans.COIN_TYPE, adapter.getData().get(position).getP_name())
                    .navigation();
        });

        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                chooseWallet = position;
                if (scrollView != null) {
                    scrollView.fling(0);
                    scrollView.smoothScrollTo(0, 0);
                }
                chooseWalletType = position + 1;
                smartRefreshView.autoRefresh();
                viewpager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                chooseWalletType = position + 1;
                smartRefreshView.autoRefresh();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void getWalletData() {
        mPresenter.getWalletDetail(chooseWalletType);
    }

    @Override
    public void initData() {
        smartRefreshView.setOnRefreshListener(refreshLayout -> getWalletData());
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
        }
    }

    public void getWalletDetailOK(WalletDetailBean data) {
        if (data == null) {
            return;
        }
        adapter.setNewData(data.getList());
        walletAssetBean = new WalletAssetBean();
        walletAssetBean.setWalletType(String.valueOf(chooseWalletType));
        walletAssetBean.setToto_price(data.getToto_price());
        walletAssetBean.setToto_cny_pric(data.getToto_cny_pric());
        walletAssetBean.setPrice_tot(data.getPrice_tot());
        walletAssetBean.setTotol(data.getTotol());
        RxBus.getDefault().post(walletAssetBean);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, code = RxBusCode.OPEN_EYE)
    public void openEye() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, code = RxBusCode.CLOSE_EYE)
    public void closeEye() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

}
